package server

import (
	"github.com/go-kratos/kratos/contrib/registry/nacos/v2"
	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	"github.com/nacos-group/nacos-sdk-go/vo"
	"hello-kratos/kratos-server/internal/conf"
	"time"
)

func NewRegistry(registry *conf.Nacos_Registry) *nacos.Registry {
	sc := []constant.ServerConfig{
		*constant.NewServerConfig(registry.Server.Addr, registry.Server.Port),
	}
	cc := constant.ClientConfig{
		NamespaceId:         registry.NamespaceId,
		Username:            registry.Server.Username,
		Password:            registry.Server.Password,
		TimeoutMs:           uint64(time.Second * 5),
		NotLoadCacheAtStart: true,
		LogDir:              "/tmp/nacos/log",
		CacheDir:            "/tmp/nacos/cache",
	}
	client, err := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: sc,
		},
	)
	if err != nil {
		panic(err)
	}
	return nacos.New(client, nacos.WithGroup(registry.Group))
}
